package com.kclm.xsap.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kclm.xsap.consts.OperateType;
import com.kclm.xsap.entity.MemberBindRecordEntity;
import com.kclm.xsap.entity.MemberCardEntity;
import com.kclm.xsap.entity.MemberLogEntity;
import com.kclm.xsap.entity.RechargeRecordEntity;
import com.kclm.xsap.service.MemberBindRecordService;
import com.kclm.xsap.service.MemberCardService;
import com.kclm.xsap.service.MemberLogService;
import com.kclm.xsap.service.RechargeRecordService;
import com.kclm.xsap.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/cardBind")
public class MemberBindRecordController {
    @Autowired
    private MemberBindRecordService memberBindRecordService;
    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private MemberLogService memberLogService;
    @Autowired
    private RechargeRecordService rechargeRecordService;

    /**
     * 跳转到会员绑卡页面
     */
    @GetMapping("/x_member_card_bind.do")
    public String memberCard() {
        return "member/x_member_card_bind";
    }


    /**
     * 绑卡操作
     */
    @PostMapping("/memberBind.do")
    @ResponseBody
    public R memberBind(@Valid MemberBindRecordEntity memberBindRecordEntity, BindingResult bindingResult, @RequestParam("operator") String operator) {
        System.out.println(memberBindRecordEntity);
        if (bindingResult.hasErrors()) {
            HashMap<String, String> errorMap = new HashMap<>();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return R.error(400, "非法参数").put("errorMap", errorMap);
        }
        System.out.println("绑定记录信息：" + memberBindRecordEntity);
        MemberBindRecordEntity isNUll = memberBindRecordService.getOne(new QueryWrapper<MemberBindRecordEntity>()
                .eq("member_id", memberBindRecordEntity.getMemberId())
                .eq("card_id", memberBindRecordEntity.getCardId()));
        //判断是否重复绑卡
        if (isNUll != null) {
            return R.error("您已经绑定过此卡！");
        }
        MemberCardEntity cardEntity = memberCardService.getById(memberBindRecordEntity.getCardId());
        Integer validCount = memberBindRecordEntity.getValidCount();
        Integer validDay = memberBindRecordEntity.getValidDay();
        //会员卡最终值
        memberBindRecordEntity.setValidCount(cardEntity.getTotalCount() + validCount)
                .setValidDay(cardEntity.getTotalDay() + validDay);
        boolean flag = memberBindRecordService.save(memberBindRecordEntity);
        if (flag) {
            //绑卡操作日志
            MemberBindRecordEntity bindRecordEntity = memberBindRecordService.getOne(new QueryWrapper<MemberBindRecordEntity>()
                    .eq("member_id", memberBindRecordEntity.getMemberId())
                    .eq("card_id", memberBindRecordEntity.getCardId()));
            MemberLogEntity memberLogBindEntity = new MemberLogEntity();
            MemberLogEntity memberLogBindAddEntity = new MemberLogEntity();
            RechargeRecordEntity rechargeRecordEntity = new RechargeRecordEntity();
            //会员日志绑定信息
            memberLogBindEntity.setType(OperateType.BINDING_CARD_OPERATION.getMsg())
                    .setInvolveMoney(cardEntity.getPrice())
                    .setOperator(operator)
                    .setMemberBindId(bindRecordEntity.getId())
                    .setCardCountChange(cardEntity.getTotalCount())
                    .setCardDayChange(cardEntity.getTotalDay())
                    .setNote(memberBindRecordEntity.getNote());
            memberLogBindAddEntity.setType(OperateType.BIND_CARD_RECHARGE_OPERATION.getMsg())
                    .setInvolveMoney(memberBindRecordEntity.getReceivedMoney())
                    .setOperator(operator)
                    .setMemberBindId(bindRecordEntity.getId())
                    .setCardCountChange(validCount)
                    .setCardDayChange(validDay)
                    .setNote(memberBindRecordEntity.getNote());
            rechargeRecordEntity.setAddCount(validCount)
                    .setAddDay(validDay)
                    .setReceivedMoney(memberBindRecordEntity.getReceivedMoney())
                    .setPayMode(memberBindRecordEntity.getPayMode())
                    .setOperator(operator)
                    .setNote(memberBindRecordEntity.getNote())
                    .setMemberBindId(bindRecordEntity.getId());
            if (memberLogService.save(memberLogBindEntity) && memberLogService.save(memberLogBindAddEntity) && rechargeRecordService.save(rechargeRecordEntity)) {
                return R.ok();
            } else {
                return R.error("添加失败,请重试");
            }
        } else {
            return R.error("添加失败,请重试");
        }
    }

}
